package com.gxa.dao.impl;

import com.gxa.dao.OwnerDao;
import com.gxa.entity.Build;
import com.gxa.entity.Owner;
import com.gxa.utils.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 业主 数据访问层 实现类
 */
public class OwnerDaoImpl implements OwnerDao {
    @Override
    public List<Owner> selectAllOwner() throws SQLException {
        // 1、获取连接
        Connection connection = DBConnection.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Owner> ownerList = new ArrayList<>();
        try {
            // 2、准备SQL
            String sql = "SELECT oid, oname, tel, card, sex, birthday, job, type FROM owner";
            // 3、获取预编译对象，预编译SQL
            ps = connection.prepareStatement(sql);
            // 4、传入参数
            // 5、执行SQL语句
            rs = ps.executeQuery();
            // 6、处理查询结果集
            while (rs.next()){
                int oid = rs.getInt("oid");
                String oname = rs.getString("oname");
                String tel = rs.getString("tel");

                Owner owner = new Owner(oid,oname,tel,null,null,null,null,null);
                ownerList.add(owner);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 7、释放资源
            DBConnection.close(rs,ps,connection);
        }

        return ownerList;
    }
}
